﻿Imports System.Resources
Imports System.Threading
Imports Biz
Imports BLL
Imports DevComponents.DotNetBar


Public Class ctrlNuevoEmpleado


    Private _llamador As frmMainMetro
    Private _bAlta As Boolean
    Private _oEmpleado As cEmpleado
    Private _RM As ResourceManager


    Public Sub SetIdioma()

        _RM = New ResourceManager("UI.Idiomas", System.Reflection.Assembly.GetExecutingAssembly)

        btnAceptar.Text = _RM.GetString("btnAceptar")
        btnCancelar.Text = _RM.GetString("btnCancelar")

        lblTituloLegajo.Text = _RM.GetString("strLegajo") + ":"
        lblTituloNombre.Text = _RM.GetString("strNombre") + ":"
        lblTituloInterno.Text = _RM.GetString("strInterno") + ":"
        lblTituloEMail.Text = _RM.GetString("strEMail") + ":"
        lblTituloDepartamento.Text = _RM.GetString("strDepartamento") + ":"

        lblTituloForm.Text = _RM.GetString("strNuevoEmpleado")

    End Sub


    Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        _llamador.CloseModalPanel(Me, DevComponents.DotNetBar.Controls.eSlideSide.Left)
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub CargarComboDepartamento()
        Dim oDepartamento As cDepartamento
        Dim colObjetos As Collection
        Dim cmbItem As ComboBoxItem


        _RM = New ResourceManager("UI.Idiomas", System.Reflection.Assembly.GetExecutingAssembly)

        cmbDepartamento.BeginUpdate()

        cmbDepartamento.Items.Clear()

        cmbItem = New ComboBoxItem

        cmbItem.Tag = ""
        cmbItem.Text = _RM.GetString("strComboSeleccioneItem")

        cmbDepartamento.Items.Add(cmbItem)

        Try
            colObjetos = GestorDepartamento.Instance.Consultar

            If Not colObjetos Is Nothing Then

                For Each oDepartamento In colObjetos

                    cmbItem = New ComboBoxItem

                    cmbItem.Tag = oDepartamento.Dep_Codigo
                    cmbItem.Text = oDepartamento.Dep_Descrip

                    cmbDepartamento.Items.Add(cmbItem)
                Next
            End If

            cmbDepartamento.SelectedIndex = 0

        Catch ex As Excepciones.DepartamentoFalloConsultar
            Main.ManejarExcepcion(ex)
        Catch ex As Exception
            Main.ManejarExcepcion(ex)
        End Try

        cmbDepartamento.EndUpdate()

    End Sub

    Public Property Llamador() As frmMainMetro
        Get
            Return _llamador
        End Get
        Set(value As frmMainMetro)
            _llamador = value
        End Set
    End Property

    Private Function ValidarForm() As Boolean
        Dim bValido As Boolean = True

        If txtLegajo.Text.Trim = "" Then bValido = False
        If txtNombre.Text.Trim = "" Then bValido = False
        If txtInterno.Text.Trim = "" Then bValido = False
        If txtEMail.Text.Trim = "" Then bValido = False
        If cmbDepartamento.SelectedIndex <= 0 Then bValido = False

        ValidarForm = bValido

    End Function

    Public Sub MostrarParaAlta()
        _RM = New ResourceManager("UI.Idiomas", System.Reflection.Assembly.GetExecutingAssembly)

        _bAlta = True

        lblTituloForm.Text = _RM.GetString("strNuevoEmpleado")
        lblMensajes.Text = ""

        _oEmpleado = Nothing

        LlenarForm(Nothing)

        txtLegajo.Focus()

    End Sub

    Public Sub MostrarParaEdicion(oEmpleado As cEmpleado)
        _RM = New ResourceManager("UI.Idiomas", System.Reflection.Assembly.GetExecutingAssembly)

        _bAlta = False

        lblTituloForm.Text = _RM.GetString("strEditarEmpleado")
        lblMensajes.Text = ""

        _oEmpleado = oEmpleado

        LlenarForm(oEmpleado)

        txtNombre.Focus()

    End Sub

    Private Sub LlenarForm(oAux As cEmpleado)

        If Not oAux Is Nothing Then
            txtLegajo.Text = oAux.Emp_Legajo
            txtNombre.Text = oAux.Emp_NombreAp
            txtInterno.Text = oAux.Emp_NroInterno
            txtEMail.Text = oAux.Emp_Mail
            PosicionarCombo(cmbDepartamento, oAux.oDepartamento.Dep_Codigo)

        Else
            txtLegajo.Text = ""
            txtNombre.Text = ""
            txtInterno.Text = ""
            txtEMail.Text = ""
            cmbDepartamento.SelectedIndex = 0
        End If

    End Sub

    Private Sub PosicionarCombo(cmbAux As Object, sTag As String)
        Dim cmbItem As DevComponents.DotNetBar.ComboBoxItem

        If cmbAux.Items.Count > 1 Then
            For Each cmbItem In cmbDepartamento.Items
                If cmbItem.Tag = sTag Then
                    cmbAux.SelectedItem = cmbItem
                End If
            Next
        End If

    End Sub

    Private Sub btnAceptar_Click(sender As Object, e As EventArgs) Handles btnAceptar.Click
        Dim oAux As cEmpleado


        If ValidarForm() Then
            If _bAlta Then
                oAux = New cEmpleado
                oAux.Emp_Legajo = txtLegajo.Text
                oAux.Emp_NombreAp = txtNombre.Text
                oAux.Emp_NroInterno = txtInterno.Text
                oAux.Emp_Mail = txtEMail.Text
                oAux.oDepartamento.Dep_Codigo = Utils.Convert.ToInt16(cmbDepartamento.SelectedItem.tag)

                Try
                    GestorEmpleado.Instance.Ingresar(oAux)
                    GestorBitacora.Instance.Loguear(New cBitacora_Usuario(AppCurrentState.UsuarioLogueado.Usu_Legajo, Utils.Enumeradores.Bitacora.EmpleadoAlta))

                Catch ex As Excepciones.EmpleadoFalloAlta
                    Main.ManejarExcepcion(ex)
                Catch ex As Exception
                    Main.ManejarExcepcion(ex)
                End Try

            Else
                oAux = New cEmpleado
                oAux.Emp_Legajo = txtLegajo.Text
                oAux.Emp_NombreAp = txtNombre.Text
                oAux.Emp_NroInterno = txtInterno.Text
                oAux.Emp_Mail = txtEMail.Text
                oAux.oDepartamento.Dep_Codigo = Utils.Convert.ToInt16(cmbDepartamento.SelectedItem.tag)

                Try
                    GestorEmpleado.Instance.Editar(oAux)
                    GestorBitacora.Instance.Loguear(New cBitacora_Usuario(AppCurrentState.UsuarioLogueado.Usu_Legajo, Utils.Enumeradores.Bitacora.EmpleadoModificacion))

                Catch ex As Excepciones.EmpleadoFalloEditar
                    Main.ManejarExcepcion(ex)
                Catch ex As Exception
                    Main.ManejarExcepcion(ex)
                End Try

            End If

            _llamador.CloseModalPanel(Me, DevComponents.DotNetBar.Controls.eSlideSide.Left)
            _llamador.RefrescarGrillas()
        Else
            lblMensajes.Text = "Complete todos los datos requeridos..."
        End If
    End Sub

    Private Sub txtLegajo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtLegajo.KeyPress
        e.Handled = Not IsNumeric(e.KeyChar) And Not Char.IsControl(e.KeyChar)
    End Sub

    Private Sub txtInterno_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtInterno.KeyPress
        e.Handled = Not IsNumeric(e.KeyChar) And Not Char.IsControl(e.KeyChar)
    End Sub

End Class
